package com.shell.customer.base.cmd;

import com.shell.bee.acct.account.po.AcctCard;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.sequence.SequenceHelper;
import com.shell.constant.rel.ConfigConstants;
import com.shell.customer.bank.po.CoopBank;
import com.shell.customer.bank.service.CoopBankService;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.trade.service.CustomerAcctService;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DefaultBankVirtualAcctCmd implements ICommand<CusRecVo> {
	private static final String SEQUENCE_CODE = "DEFAULT_VIRTUAL_ACCT";
	@Autowired
	private CustomerAcctService customerAcctService;
	@Autowired
	private CoopBankService coopBankService;

	public boolean execute(CommandContext<CusRecVo> context) throws Exception {
		List<CoopBank> coopBankList = this.coopBankService.selectAll();
		if ((coopBankList == null) || (coopBankList.size() == 0)) {
			throw new UnCheckMsgException("��������������������������������");
		}
		String bankCode = ((CoopBank) coopBankList.get(0)).getBankCode();
		AcctCard card;
		if (!ConfigConstants.COOP_BANKS.contains(bankCode)) {
			CusRecVo cusRec = (CusRecVo) context.getSource();
			card = new AcctCard();
			card.setCustId(cusRec.getCusId());
			card.setFiNo(bankCode);
			card.setAcctType("1");
			card.setAcctSts("N");
			card.setOaaChnCode("B");
			card = this.customerAcctService.getCard(card);
			if (card == null) {
				String acctNo = SequenceHelper.generateCode("DEFAULT_VIRTUAL_ACCT", 6);
				this.customerAcctService.openBankVirtualAcct(bankCode, cusRec, acctNo);
			}
		} else {
			List<String> coopBanks = new ArrayList();
			for (CoopBank coopBank : coopBankList) {
				coopBanks.add(coopBank.getBankCode());
			}
			context.setValue("coopBanks", coopBanks);
		}
		return true;
	}
}
